<?php
declare (strict_types = 1);

namespace App\Admin\Controller;


use App\Common\Controller\AbstractController;
use App\Common\Logic\CommonLogic;
use App\Server\Model\Cate;
use App\Server\Model\CateGroup;
use Hyperf\Di\Annotation\Inject;
use App\Admin\Validate\CateValidate;

class CateController extends AbstractController
{

    /**
     * @Inject()
     * @var CateValidate
     */
    protected $CateValidate;

    public function cateIndex()
    {
        $page = $this->request->query('page');
        if ($page)
        {
            $where = [];
            $cateModel = new Cate();
            $cateList = $cateModel->getList($where,['*']);
            $cateList = (new CommonLogic())->taglist($cateList);
            foreach ($cateList as &$value){
                $value['title'] = $value['str'].$value['title'];
            }
            return $this->success($cateList, '成功');
        }
        else
        {
            return $this->render->render('/admin/cate/cate_index');
        }
    }

    public function addCate()
    {

        $page = $this->request->query('page');
        if ($page) {
            $param = $this->request->all();
            unset($param['page']);
            $errorMessage = $this->CateValidate->addCate($this->request);
            //验证部分数据合法性
            if ($errorMessage) {
                return $this->error('提交失败：' . $errorMessage);
            }
            unset($param['file']);
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s',time());
            $cateGroup = new Cate();
            $addRes = $cateGroup->dataAdd($param);
            if ($addRes){
                return $this->success([],'创建成功');
            }else{
                return $this->error('创建失败');
            }
        }
        else
        {
            $data = [];
            $cates = (new Cate())->getList([],['*']);
            $data['cate'] = (new CommonLogic())->taglist($cates);
            return $this->render->render('/admin/cate/add_cate',$data);
        }

    }


    public function editCate()
    {
        $id = $this->request->query('id');
        $cate = new Cate();
        $page = $this->request->query('page');
        if ($page) {
            $param = $this->request->all();
            unset($param['page']);
            $errorMessage = $this->CateValidate->editCate($this->request);
            //验证部分数据合法性
            if ($errorMessage) {
                return $this->error('提交失败：' . $errorMessage);
            }
            unset($param['file']);
            $cateGroup = new Cate();
            $updateRes = $cateGroup->dataUpdate([['id','=',$param['id']]],$param);
            if ($updateRes){
                return $this->success([],'修改成功');
            }else{
                return $this->error('修改失败');
            }
        }
        else
        {
            $data = [];
            $data['data'] = $cate -> getInfo([['id','=',$id]],['*']);
            $cates = (new Cate())->getList([],['*']);
            $data['cate'] = (new CommonLogic())->taglist($cates);
            return $this->render->render('/admin/cate/edit_cate', $data);
        }
    }


    public function deleteCate()
    {
        $cate = new Cate();
        $id = $this->request->query('id');
        if(!$id) {
            return $this->error('缺少必要条件');
        }
        $count = $cate->dataCount([["cate_group_id" ,'=', $id]]);
        if ($count > 0) {
            return $this->error('删除失败,该分类下还存在子分类');
        }
        $deleteRes = $cate->dataDelete([['id','=',$id]]);
        if (!$deleteRes) {
            return $this->error('删除失败！');
        }
        return $this->success([],'删除成功！');
    }
}
